package com.auto.nastoolbox.ui

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.rememberDrawerState
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.auto.base.post.AppContainer
import com.auto.nastoolbox.NasDestinations
import com.auto.nastoolbox.NasNavigationActions
import com.auto.nastoolbox.components.NasNavGraph
import com.auto.nastoolbox.theme.NasToolBoxTheme
import kotlinx.coroutines.launch

/**
 * Created by YuanWei1 on 2025/6/3.
 *
 * Description:
 */
@Composable
fun MainApp(
    appContainer: AppContainer,
    widthSizeClass: WindowWidthSizeClass
) {
    NasToolBoxTheme {
        val navController = rememberNavController()
//        全局跳转方法
        val navigationActions = remember(navController) {
            NasNavigationActions(navController)
        }
        //协程生命周期相关 代替 GlobalScope 或 lifecycleScope
        val coroutineScope = rememberCoroutineScope()
        //获取当前栈顶的NavBackStackEntry
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute =
            navBackStackEntry?.destination?.route ?: NasDestinations.HOME_ROUTE
        //是否为横屏
        val isExpandedScreen = widthSizeClass == WindowWidthSizeClass.Expanded
        val sizeAwareDrawerState = rememberSizeAwareDrawerState(isExpandedScreen)
        ModalNavigationDrawer(
            drawerContent = {
                AppDrawer(
                    currentRoute = currentRoute,
                    navigationActions,
                    closeDrawer = {
                        coroutineScope.launch { sizeAwareDrawerState.close() }
                    },
                    Modifier.fillMaxWidth(0.7f)
                )
            },
            drawerState = sizeAwareDrawerState,
            // Only enable opening the drawer via gestures if the screen is not expanded
            gesturesEnabled = !isExpandedScreen
        ) {
            Row {
                NasNavGraph(
                    appContainer = appContainer,
                    isExpandedScreen = isExpandedScreen,
                    navController = navController,
                    openDrawer = { coroutineScope.launch { sizeAwareDrawerState.open() } },
                )
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun rememberSizeAwareDrawerState(isExpandedScreen: Boolean): DrawerState {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    return if (!isExpandedScreen) {
        // If we want to allow showing the drawer, we use a real, remembered drawer
        // state defined above
        drawerState
    } else {
        // If we don't want to allow the drawer to be shown, we provide a drawer state
        // that is locked closed. This is intentionally not remembered, because we
        // don't want to keep track of any changes and always keep it closed
        DrawerState(DrawerValue.Closed)
    }
}
